/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.imagesteganographylibrary.Utils;

import ohos.media.image.PixelMap;
import ohos.media.image.common.Position;
import java.util.List;
/**
 * MergePixelMap
 *
 * @ProjectName: Image-Steganography-Library
 * @ClassName: MergePixelMap
 * @Description: pixeMap 合并
 * @Author: lr
 * @CreateDate: 2021/5/7 15:19
 * @since 2021-05-07
 *
 */
public class MergePixelMap {
    private MergePixelMap() {
    }

    /**
     * list merge pixelMap
     *
     * @param list pixelmapList
     * @param col col
     * @param opts PixelMap.InitializationOptions
     * @return pixelMap
     */
    public static PixelMap getMergePixelmap(List<PixelMapSlice> list, int col, PixelMap.InitializationOptions opts) {
        int width = opts.size.width;
        int height = opts.size.height;
        int sum = width * height;
        int[] colors = new int[sum];
        int offsetWidth = 0;
        int offsetHeight = 0;
        for (int il = 0; il < list.size(); il++) {
            PixelMapSlice pixelMapSlice = list.get(il);
            if (il % col == 0) {
                offsetWidth = 0;
                if (il != 0) {
                    offsetHeight += list.get(il - 1).height;
                }
            } else {
                offsetWidth += list.get(il - 1).width;
            }
            for (int jl = 0; jl < pixelMapSlice.height; jl++) {
                for (int kl = 0; kl < pixelMapSlice.width; kl++) {
                    int index = kl + width * jl + offsetWidth + offsetHeight * width;
                    colors[index] = pixelMapSlice.pixelMap.readPixel(new Position(kl, jl));
                }
            }
        }
        return PixelMap.create(colors, opts);
    }

    /**
     * PixelMapSlice
     *
     * @ProjectName: Image-Steganography-Library
     * @ClassName: PixelMapSlice
     * @Description: 自定义pixelMap
     * @Author: lr
     * @CreateDate: 2021/5/7 15:19
     * @since 2021-05-07
     */
    public static class PixelMapSlice {
        PixelMap pixelMap;
        int width;
        int height;
        /**
         * create PixelMapSlice
         *
         * @param width width
         * @param height height
         * @param pixelMap pixelMap
         */
        public PixelMapSlice(int width, int height, PixelMap pixelMap) {
            this.width = width;
            this.height = height;
            this.pixelMap = pixelMap;
        }
        public PixelMap getPixelMap() {
            return pixelMap;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }
        /**
         * create PixelMapSlice
         *
         * @param width width
         * @param height height
         * @param pixelMap pixelMap
         * @return PixelMapSlice
         */
        public static PixelMapSlice create(int width, int height, PixelMap pixelMap) {
            return new PixelMapSlice(width, height, pixelMap);
        }
    }
}
